class_name Enemy extends CharacterBody3D
@export var int_警觉范围 := 10
@export var attack_range := 1.5
@export var max_hitpoints := 100 ##最大生命值
@onready var navigation_agent_3d: NavigationAgent3D = $NavigationAgent3D
@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var player: Node3D = get_tree().get_first_node_in_group("player")

const SPEED = 5.0
var bool_警觉: bool = false
var hitpoints := max_hitpoints:
	set(new_hitpoints):
		hitpoints = new_hitpoints
		if hitpoints <= 0:
			queue_free()
			
			
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity")
func _process(delta: float) -> void:
	pass

var 测试 := 0

func _physics_process(delta: float) -> void:
	var distance = global_position.distance_to(player.global_position)
	if distance <= int_警觉范围:
		bool_警觉 = true
	if bool_警觉:
		navigation_agent_3d.target_position = player.global_position	
		var next_position := navigation_agent_3d.get_next_path_position()
		var direction = global_position.direction_to(next_position)
		
		look_at_target(direction)
		
		if distance <= attack_range:
			animation_player.play("Attack")
		if direction:
			velocity.x = direction.x * SPEED
			velocity.z = direction.z * SPEED
		else:
			velocity.x = move_toward(velocity.x, 0, SPEED)
			velocity.z = move_toward(velocity.z, 0, SPEED)

		move_and_slide()
func attack() -> void:
	player.hitpoints -= 10

func look_at_target(direction:Vector3) -> void:
	var adjust_direction = direction
	adjust_direction.y = 0
	look_at(global_position + adjust_direction,Vector3.UP,true)
